package vcp.client.GUI.mvc.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import vcp.client.GUI.events.FormSubmitionEvent;
import vcp.client.forms.ComplaintForm;
import vcp.client.interfaces.listeners.IFormSubmitionListener;
import vcp.client.utility.Common;
import vcp.client.utility.UtilityHelper;
import vcp.entity.Complaint;
import vcp.enums.ComplaintType;
import vcp.enums.ComplaintType.ComplaintStatus;
import vcp.enums.SiteEnum;

/**
 * <p>
 * user interface fill complaint screen class
 * </p>
 * 
 * @author VCP team
 * 
 */
public class UIFillComplaintView extends UIAbstractView implements KeyListener, ItemListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1758734714515777890L;

	private JButton btnSubmit;
	private JComboBox <ComplaintType> cbComplaintType;
	private JTextArea taComplaintText;

	private IFormSubmitionListener <ComplaintForm> formSubmitionListener;
	private JLabel lblComplaintStatus;
	private JComboBox <Integer> cbComplaintsID;
	private JTextField tfOpenDate;

	private Integer selectedComplaintID;
	private JTextArea taResolveText;
	private JComboBox <SiteEnum> cbSiteEnum;

	/**
	 * setter
	 * 
	 * @param formSubmitionListener
	 */
	public void setFormSubmitionListener ( IFormSubmitionListener <ComplaintForm> formSubmitionListener )
	{
		this.formSubmitionListener = formSubmitionListener;
	}

	/**
	 * constructor
	 */
	public UIFillComplaintView ()
	{
		super();
		this.selectedComplaintID = 0;
	}

	/****************************************************************************************************************************
	 *** Public methods ***
	 ****************************************************************************************************************************/
	/**
	 * getter
	 * 
	 * @return selected complaint id
	 */
	public Integer getCurrentComplaintID ()
	{
		return this.selectedComplaintID;
	}

	/**
	 * method adds all complaints
	 * 
	 * @param complaintsList
	 */
	public void addAllComplaintIDs ( List <Complaint> complaintsList )
	{
		this.cbComplaintsID.removeAllItems();
		for ( Complaint complaint : complaintsList )
		{
			Integer complaintID = complaint.getID();
			this.cbComplaintsID.addItem(complaintID);
		}

		if ( complaintsList.size() > 0 )
		{
			this.selectedComplaintID = complaintsList.get(0).getID();
			this.cbComplaintsID.setSelectedIndex(0);
			this.cbComplaintsID.setEnabled(true);
		}
		else
		{
			this.selectedComplaintID = 0;
			this.cbComplaintsID.setEnabled(false);
		}
	}

	/**
	 * method loads complaint details
	 * 
	 * @param complaint
	 */
	public void loadComplaint ( Complaint complaint )
	{
		if ( complaint != null )
		{
			this.selectedComplaintID = complaint.getID();
			this.lblComplaintStatus.setText(complaint.getStatus().toString());
			this.lblComplaintStatus.setForeground(this.getComplaintStatusColorCoded(complaint.getStatus()));

			DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm");
			this.tfOpenDate.setText(dtf.print(complaint.getOpenDate()));

			String txt = UtilityHelper.getUtilityHelperInstance().isStringNullOrEmpty(complaint.getText())
																											? ""
																											: complaint.getText();
			this.taComplaintText.setText(txt);

			if ( complaint.getResolveText() != null )
			{
				this.taResolveText.setText(complaint.getResolveText());
			}
		}
		else
		{
			this.cbComplaintsID.removeAllItems();
			this.cbComplaintsID.setEnabled(false);
		}
	}

	/**
	 * method adds complaint id
	 * 
	 * @param id
	 */
	public void addComplaintID ( Integer id )
	{
		DefaultComboBoxModel <Integer> model = (DefaultComboBoxModel <Integer>) this.cbComplaintsID.getModel();
		if ( model.getIndexOf(id) == - 1 )
		{
			model.addElement(id);
		}
		this.cbComplaintsID.setEnabled(true);
	}

	/**
	 * getter
	 * 
	 * @param status
	 * @return complaint status color
	 */

	private Color getComplaintStatusColorCoded ( ComplaintStatus status )
	{
		Color textColor = null;
		switch ( status )
		{
			case OPENED:
				textColor = Color.RED;
				break;
			case CLOSED:
				textColor = Color.GREEN;
				break;
			default:
				textColor = Color.YELLOW;
				break;
		}
		return textColor;
	}

	/**
	 * getter
	 * 
	 * @return site
	 */
	private SiteEnum getSelectedSite ()
	{
		return (SiteEnum) this.cbSiteEnum.getSelectedItem();
	}

	/****************************************************************************************************************************
	 *** UIAbstractView methods ***
	 ****************************************************************************************************************************/
	@Override
	public void clearView ()
	{
		this.resetView();
		this.btnSubmit.setEnabled(false);
		this.onScreen = false;
		this.selectedComplaintID = 0;
		this.cbComplaintsID.setEnabled(false);
	}

	@Override
	public void resetView ()
	{
		this.taComplaintText.setText("");
		this.cbComplaintType.setSelectedIndex(0);

		this.cbComplaintsID.removeAllItems();
		this.lblComplaintStatus.setText("");
		this.tfOpenDate.setText("");
	}

	@Override
	protected void setupLayout ()
	{
		this.setPreferredSize(new Dimension(904, 708));

		this.setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		this.add(panel, BorderLayout.NORTH);
		panel.setLayout(new GridLayout(0, 4, 0, 0));

		JPanel panel_5 = new JPanel();
		panel.add(panel_5);
		FlowLayout flowLayout_1 = (FlowLayout) panel_5.getLayout();
		flowLayout_1.setAlignment(FlowLayout.RIGHT);

		JLabel lblComplaintType = new JLabel("Complaint Type:");
		lblComplaintType.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		panel_5.add(lblComplaintType);

		JPanel panel_6 = new JPanel();
		panel.add(panel_6);
		FlowLayout flowLayout = (FlowLayout) panel_6.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);

		this.cbComplaintType = new JComboBox <>();
		this.cbComplaintType.setFocusable(false);
		this.cbComplaintType.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		this.cbComplaintType.setModel(new DefaultComboBoxModel <>(ComplaintType.values()));
		panel_6.add(this.cbComplaintType);

		JPanel panel_23 = new JPanel();
		FlowLayout flowLayout_9 = (FlowLayout) panel_23.getLayout();
		flowLayout_9.setAlignment(FlowLayout.LEFT);
		panel.add(panel_23);

		JLabel lblSite = new JLabel("Site:");
		lblSite.setFont(new Font("Dialog", Font.PLAIN, 18));
		panel_23.add(lblSite);

		this.cbSiteEnum = new JComboBox <>();
		this.cbSiteEnum.setFont(new Font("Dialog", Font.PLAIN, 18));
		this.cbSiteEnum.setModel(new DefaultComboBoxModel <SiteEnum>(SiteEnum.values()));
		panel_23.add(this.cbSiteEnum);

		JPanel panel_1 = new JPanel();
		this.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));

		JPanel panel_7 = new JPanel();
		panel_7.setPreferredSize(new Dimension(226, 10));
		panel_1.add(panel_7, BorderLayout.WEST);
		panel_7.setLayout(new GridLayout(10, 1, 0, 0));

		JPanel panel_2 = new JPanel();
		FlowLayout flowLayout_2 = (FlowLayout) panel_2.getLayout();
		flowLayout_2.setAlignment(FlowLayout.RIGHT);
		panel_7.add(panel_2);

		JLabel lblText = new JLabel("Text:");
		panel_2.add(lblText);
		lblText.setFont(new Font("Lucida Grande", Font.PLAIN, 18));

		JPanel panel_9 = new JPanel();
		panel_7.add(panel_9);

		JPanel panel_10 = new JPanel();
		panel_7.add(panel_10);

		JPanel panel_11 = new JPanel();
		panel_7.add(panel_11);

		JPanel panel_12 = new JPanel();
		panel_7.add(panel_12);

		JPanel panel_13 = new JPanel();
		FlowLayout flowLayout_6 = (FlowLayout) panel_13.getLayout();
		flowLayout_6.setVgap(19);
		flowLayout_6.setAlignment(FlowLayout.RIGHT);
		panel_7.add(panel_13);

		JLabel lblComplaints = new JLabel("Complaints:");
		lblComplaints.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_13.add(lblComplaints);

		JPanel panel_18 = new JPanel();
		FlowLayout flowLayout_7 = (FlowLayout) panel_18.getLayout();
		flowLayout_7.setVgap(10);
		flowLayout_7.setAlignment(FlowLayout.RIGHT);
		panel_7.add(panel_18);

		JLabel lblOpenDate = new JLabel("Open Date:");
		lblOpenDate.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_18.add(lblOpenDate);

		JPanel panel_20 = new JPanel();
		FlowLayout flowLayout_8 = (FlowLayout) panel_20.getLayout();
		flowLayout_8.setVgap(20);
		flowLayout_8.setAlignment(FlowLayout.RIGHT);
		panel_7.add(panel_20);

		JLabel lblNewLabel = new JLabel("Resolve Text:");
		lblNewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		panel_20.add(lblNewLabel);

		JPanel panel_3 = new JPanel();
		panel_1.add(panel_3, BorderLayout.CENTER);
		panel_3.setLayout(new GridLayout(2, 0, 0, 0));

		JPanel panel_4 = new JPanel();
		panel_3.add(panel_4);
		panel_4.setLayout(new BorderLayout(0, 0));

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		panel_4.add(scrollPane);

		this.taComplaintText = new JTextArea();
		this.taComplaintText.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		scrollPane.setViewportView(this.taComplaintText);
		this.taComplaintText.setRows(10);

		JPanel panel_19 = new JPanel();
		panel_4.add(panel_19, BorderLayout.WEST);

		JPanel panel_14 = new JPanel();
		panel_3.add(panel_14);
		panel_14.setLayout(new BorderLayout(0, 0));

		JPanel panel_15 = new JPanel();
		panel_14.add(panel_15, BorderLayout.NORTH);
		panel_15.setLayout(new GridLayout(2, 1, 0, 0));

		JPanel panel_16 = new JPanel();
		FlowLayout flowLayout_5 = (FlowLayout) panel_16.getLayout();
		flowLayout_5.setVgap(14);
		flowLayout_5.setAlignment(FlowLayout.LEFT);
		panel_15.add(panel_16);

		this.cbComplaintsID = new JComboBox <>();
		this.cbComplaintsID.setEnabled(false);
		this.cbComplaintsID.setModel(new DefaultComboBoxModel <Integer>());
		this.cbComplaintsID.setFocusable(false);
		this.cbComplaintsID.setPreferredSize(new Dimension(85, 30));
		this.cbComplaintsID.setFont(new Font("Tahoma", Font.PLAIN, 18));
		panel_16.add(this.cbComplaintsID);

		this.lblComplaintStatus = new JLabel("");
		panel_16.add(this.lblComplaintStatus);
		this.lblComplaintStatus.setFont(new Font("Tahoma", Font.PLAIN, 18));

		JPanel panel_17 = new JPanel();
		FlowLayout flowLayout_4 = (FlowLayout) panel_17.getLayout();
		flowLayout_4.setAlignment(FlowLayout.LEFT);
		panel_15.add(panel_17);

		this.tfOpenDate = new JTextField();
		panel_17.add(this.tfOpenDate);
		this.tfOpenDate.setFont(new Font("Tahoma", Font.PLAIN, 18));
		this.tfOpenDate.setFocusable(false);
		this.tfOpenDate.setEnabled(false);
		this.tfOpenDate.setColumns(10);

		JPanel panel_21 = new JPanel();
		panel_14.add(panel_21, BorderLayout.EAST);

		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setPreferredSize(new Dimension(4, 80));
		panel_14.add(scrollPane_1, BorderLayout.CENTER);

		this.taResolveText = new JTextArea();
		this.taResolveText.setEnabled(false);
		this.taResolveText.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		scrollPane_1.setViewportView(this.taResolveText);

		JPanel panel_22 = new JPanel();
		panel_14.add(panel_22, BorderLayout.WEST);

		JPanel panel_8 = new JPanel();
		FlowLayout flowLayout_3 = (FlowLayout) panel_8.getLayout();
		flowLayout_3.setAlignment(FlowLayout.RIGHT);
		panel_1.add(panel_8, BorderLayout.SOUTH);

		this.btnSubmit = new JButton(Common.BTN_SUBMIT);
		panel_8.add(this.btnSubmit);
		this.btnSubmit.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		this.btnSubmit.setName(Common.BTN_SUBMIT);
		this.btnSubmit.setPreferredSize(new Dimension(175, 75));
		this.btnSubmit.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
		this.btnSubmit.setEnabled(false);
	}

	@Override
	protected void setupListeners ()
	{
		this.taComplaintText.addKeyListener(this);
		this.btnSubmit.addActionListener(this);
		this.cbComplaintsID.addItemListener(this);
	}

	@Override
	protected void setupButtonsIcons ()
	{
		int iconTextGap = 20;

		ImageIcon icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_SUBMIT, 40, 40);
		if ( icon != null )
		{
			this.btnSubmit.setIcon(icon);
			this.btnSubmit.setIconTextGap(iconTextGap);
		}
	}

	/****************************************************************************************************************************
	 *** ActionListener methods ***
	 ****************************************************************************************************************************/

	@Override
	public void actionPerformed ( ActionEvent actionEvent )
	{
		if ( this.formSubmitionListener != null )
		{
			ComplaintType type = (ComplaintType) this.cbComplaintType.getSelectedItem();
			String complaintText = this.taComplaintText.getText();
			DateTime openDate = this.controllerDelegate.requestCurrentClientDateTime();

			this.formSubmitionListener.onFormSubmitted(new FormSubmitionEvent <ComplaintForm>(this, new ComplaintForm(type, complaintText, openDate, this.getSelectedSite())));
		}
	}

	/****************************************************************************************************************************
	 *** KeyListener methods ***
	 ****************************************************************************************************************************/
	@Override
	public void keyTyped ( KeyEvent e )
	{
	}

	@Override
	public void keyPressed ( KeyEvent e )
	{
	}

	@Override
	public void keyReleased ( KeyEvent e )
	{
		String textAreaText = this.taComplaintText.getText();
		this.btnSubmit.setEnabled(textAreaText.length() > 0);
	}

	/****************************************************************************************************************************
	 *** ItemListener methods ***
	 ****************************************************************************************************************************/
	@Override
	public void itemStateChanged ( ItemEvent event )
	{
		if ( event.getStateChange() == ItemEvent.SELECTED )
		{
			if ( this.onScreen )
			{
				Integer selectedComplaintID = (Integer) event.getItem();
				if ( selectedComplaintID != this.selectedComplaintID )
				{
					this.selectedComplaintID = selectedComplaintID;
					this.controllerDelegate.onButtonPress(selectedComplaintID);
				}
			}
		}
	}

}
